package gs.android.bingomathfree;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class SettingsActivity extends Activity {
	private SharedPreferences sp;
	private RadioGroup level, mode;
	private EditText time;
	private Button save;
	
	@Override
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.settings);
		
		save = (Button) findViewById(R.id.btnSave);
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					int l = level.getCheckedRadioButtonId();
					int m = mode.getCheckedRadioButtonId();
					int t = Integer.parseInt(time.getText().toString());
					Editor editor = sp.edit();
					
					int lvl = 0;
					switch (l) {
					case R.id.rbnEasy:
						lvl = BingoActivity.EASY;
						break;
					case R.id.rbnNormal:
						lvl = BingoActivity.NORMAL;
						break;
					case R.id.rbnHard:
						lvl = BingoActivity.HARD;
						break;
					}
					editor.putInt("level", lvl);
					
					boolean bool = true;
					if (m == R.id.rbnRounds)
						bool = false;
					editor.putBoolean("gameMode", bool);
					editor.putInt("time", t);
					
					editor.commit();
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(), getResources().getString(R.string.settingerror), Toast.LENGTH_LONG).show();
				} finally {
					SettingsActivity.this.finish();
				}
			}
		});
		
		level = (RadioGroup) findViewById(R.id.rgpLevel);
		mode = (RadioGroup) findViewById(R.id.rgpGameMode);
		time = (EditText) findViewById(R.id.txtTime);
		
		sp = getSharedPreferences("bingo", MODE_PRIVATE);
		int lvl = sp.getInt("level", BingoActivity.EASY);
		boolean b = sp.getBoolean("gameMode", true);
		int t = sp.getInt("time", 15);
		
		RadioButton r = null;
		switch (lvl) {
		case BingoActivity.EASY:
			r = (RadioButton)level.getChildAt(0);
			break;
		case BingoActivity.NORMAL:
			r = (RadioButton)level.getChildAt(1);
			break;
		case BingoActivity.HARD:
			r = (RadioButton)level.getChildAt(2);
			break;
		}
		if (r != null)
			r.setChecked(true);
		
		mode = (RadioGroup) findViewById(R.id.rgpGameMode);
		mode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rbnTime:
					time.setEnabled(true);
					break;
				case R.id.rbnRounds:
					time.setEnabled(false);
					break;
				}
			}
		});
		
		RadioButton rb = null;
		if (b) {
			rb = (RadioButton) mode.getChildAt(0);
		} else {
			rb = (RadioButton) mode.getChildAt(1);
			time.setEnabled(false);
		}
		rb.setChecked(true);
		time.setText("" + t);
	}
}
